package com.zyzd.retrofit.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zyzd.retrofit.R;

import java.util.List;

/**
 * Created by lzy on 2018/5/3 0003.
 * description ${列表Fragment 基类}
 */

public abstract class BaseRecyclerFragment<P extends BaseRecyclerContract.IPresenter, A extends BaseAdapter, T> extends BaseMvpFragment<P>
        implements BaseRecyclerContract.IView<A, T>, OnLoadMoreListener, OnRefreshListener, OnItemClickListener, OnItemLongClickListener {

    protected A mAdapter;
    protected RecyclerView mRecyclerView;
    protected SmartRefreshLayout mRefreshLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.layout_recycler;
    }

    @Override
    protected void initData() {
    }

    @Override
    public void finishCreateView(Bundle state) {
        super.finishCreateView(state);
        mRecyclerView = mRootView.findViewById(R.id.recyclerView);
        mRefreshLayout = mRootView.findViewById(R.id.refreshLayout);

        if (mRecyclerView != null) {
            mRecyclerView.setLayoutManager(getLayoutManager());
            mAdapter = createAdapter();
            mAdapter.setOnItemClickListener(this);
            mAdapter.setOnItemLongClickListerner(this);
            mRecyclerView.setAdapter(mAdapter);
            RecyclerView.ItemDecoration decor = getDividerItemDecoration();
            if (decor != null) mRecyclerView.addItemDecoration(decor);
        }

        if (mRefreshLayout != null) {
            mRefreshLayout.setOnLoadMoreListener(this);
            mRefreshLayout.setOnRefreshListener(this);
            mRefreshLayout.setEnableRefresh(isEnableRefresh());
            mRefreshLayout.setEnableLoadMore(isEnableLoadMore());

            if (isAutoRefresh()) {
                if (isEnableRefresh()) {
                    mRefreshLayout.autoRefresh(0, 10, 1);
                } else {
                    onRefresh(mRefreshLayout);
                }
            }
        }
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    @Override
    public RecyclerView.ItemDecoration getDividerItemDecoration() {
        return null;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPresenter.onRefresh();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mPresenter.onLoadMore();
    }

    @Override
    public void onLoadDataSuccess(boolean isRefresh, List<T> data) {
        mAdapter.addAll(isRefresh, data);
        onFinishLoad();
    }

    @Override
    public void onLoadDataFail(boolean isRefresh, String msg) {
        showToast(msg);
        onFinishLoad();
    }

    @Override
    public void onFinishLoad() {
        if (mRefreshLayout == null) return;
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void setEnableLoadMore(boolean enable) {
        mRefreshLayout.setEnableLoadMore(enable);
    }


    @Override
    public boolean isEnableRefresh() {
        return true;
    }

    @Override
    public boolean isAutoRefresh() {
        return true;
    }

    @Override
    public boolean isEnableLoadMore() {
        return true;
    }

    @Override
    public void onItemClick(int position) {
    }

    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }
}
